package com.zh.note.huawei.twohundred.快递投放问题;

import java.io.IOException;
import java.util.*;

/**
 * @Author: zhanghuan
 * @date: 2023/8/14 10:30
 * @description: create it
 */
public class Main {
    public static void main(String[] args) throws IOException {
        // BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        // String[] line1 = br.readLine().split(" ");
        // int m = Integer.parseInt(line1[0]);
        // int n = Integer.parseInt(line1[1]);

        List<String[]> wantList = new ArrayList<>();
        wantList.add(new String[]{"package1", "A", "C"});
        wantList.add(new String[]{"package2", "A", "C"});
        wantList.add(new String[]{"package3", "B", "C"});
        wantList.add(new String[]{"package4", "A", "C"});
        List<String[]> canList = new ArrayList<>();
        canList.add(new String[]{"A", "B", "package1"});
        canList.add(new String[]{"A", "C", "package2", "package4"});

        // for (int i = 0; i < m; i++) {
        //     String[] arr = br.readLine().split(" ");
        //     want.add(arr);
        // }
        // for (int i = 0; i < n; i++) {
        //     String[] arr = br.readLine().split(" ");
        //     can.add(arr);
        // }
        String res = solution(wantList, canList);
        System.out.println(res);

    }

    private static String solution(List<String[]> wantList, List<String[]> canList) {
        // key是路径，value是哪些包裹
        Map<String, Set<String>> wantMap = new HashMap<>();
        Map<String, Set<String>> canMap = new HashMap<>();

        for (String[] arr : wantList) {
            String packageName = arr[0];
            String path = arr[1] + "-" + arr[2];
            if (!wantMap.containsKey(path)) {
                wantMap.put(path, new HashSet<>());
            }
            wantMap.get(path).add(packageName);
        }

        for (String[] arr : canList) {
            String path = arr[0] + "-" + arr[1];
            if (!canMap.containsKey(path)) {
                canMap.put(path, new HashSet<>());
            }
            for (int i = 2; i < arr.length; i++) {
                canMap.get(path).add(arr[i]);
            }
        }

        List<String> res = new ArrayList<>();
        for (Map.Entry<String, Set<String>> entry : wantMap.entrySet()) {

            String path = entry.getKey();
            Set<String> wantPkg = entry.getValue();
            Set<String> canPkg = canMap.get(path);
            if (canPkg == null) {
                continue;
            }
            for (String pkg : wantPkg) {
                if (canPkg.contains(pkg)) {
                    res.add(pkg);
                }
            }
            if (res.isEmpty()) {
                return "none";
            }
            Collections.sort(res, (o1, o2) -> Integer.parseInt(o1.substring(7)) - Integer.parseInt(o2.substring(7)));

        }
        //把list转化成字符串
        return String.join(" ", res);
    }
}
